This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 08-19 05:29:05] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 08-19 05:29:05] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-19 05:29:05] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 08-19 05:29:05] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 08-19 05:29:05] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.265781, 'x2': 0.282388, 'x3': 0.413721, 'x4': 0.216239, 'x5': 0.005664, 'x6': 0.607898}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.622603, 0.1), 'l2norm': (0.677102, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.830815, 'x2': 0.655437, 'x3': 0.726567, 'x4': 0.977401, 'x5': 0.406643, 'x6': 0.414182}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.007433, 0.1), 'l2norm': (1.799409, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.647157, 'x2': 0.232718, 'x3': 0.876529, 'x4': 0.320853, 'x5': 0.835877, 'x6': 0.313499}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (0.017111, 0.1), 'l2norm': (1.487125, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.26259, 'x2': 0.625496, 'x3': 0.388924, 'x4': 0.806629, 'x5': 0.844457, 'x6': 0.247512}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.374909, 0.1), 'l2norm': (1.481698, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.510206, 'x2': 0.14549, 'x3': 0.162445, 'x4': 0.466738, 'x5': 0.391006, 'x6': 0.186119}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.464035, 0.1), 'l2norm': (1.017388, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.990729, 'x2': 0.140827, 'x3': 0.525366, 'x4': 0.892147, 'x5': 0.425175, 'x6': 0.468124}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (0.005171, 0.1), 'l2norm': (1.708342, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.628235, 'x2': 0.403889, 'x3': 0.135917, 'x4': 0.999698, 'x5': 0.613743, 'x6': 0.929683}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.034099, 0.1), 'l2norm': (1.601966, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.904304, 'x2': 0.668022, 'x3': 0.137849, 'x4': 0.464625, 'x5': 0.748459, 'x6': 0.20965}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.001698, 0.1), 'l2norm': (1.496061, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.941626, 'x2': 0.579357, 'x3': 0.271404, 'x4': 0.853287, 'x5': 0.313063, 'x6': 0.867724}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.039847, 0.1), 'l2norm': (1.658079, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.516916, 'x2': 0.806345, 'x3': 0.201589, 'x4': 0.866509, 'x5': 0.214841, 'x6': 0.595567}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.077773, 0.1), 'l2norm': (1.476056, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.324797, 'x2': 0.099296, 'x3': 0.424187, 'x4': 0.657135, 'x5': 0.67345, 'x6': 0.879864}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.012112, 0.1), 'l2norm': (1.412471, 0.1)}.
[INFO 08-19 05:29:05] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.820241, 'x2': 0.692284, 'x3': 0.394313, 'x4': 0.460051, 'x5': 0.810408, 'x6': 0.952355}.
[INFO 08-19 05:29:05] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.112802, 0.1), 'l2norm': (1.791754, 0.1)}.
[INFO 08-19 05:29:07] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.308142, 'x2': 0.232333, 'x3': 0.317521, 'x4': 0.257176, 'x5': 0.082511, 'x6': 0.444592}.
[INFO 08-19 05:29:07] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.905085, 0.1), 'l2norm': (0.711184, 0.1)}.
[INFO 08-19 05:29:15] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.283815, 'x2': 0.193521, 'x3': 0.250477, 'x4': 0.247737, 'x5': 0.077791, 'x6': 0.334122}.
[INFO 08-19 05:29:15] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.48354, 0.1), 'l2norm': (0.595921, 0.1)}.
[INFO 08-19 05:29:18] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.361398, 'x2': 0.252525, 'x3': 0.350082, 'x4': 0.283592, 'x5': 0.127002, 'x6': 0.497474}.
[INFO 08-19 05:29:18] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.378075, 0.1), 'l2norm': (0.922208, 0.1)}.
[INFO 08-19 05:29:19] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.400422, 'x2': 0.263254, 'x3': 0.368218, 'x4': 0.298661, 'x5': 0.155745, 'x6': 0.52077}.
[INFO 08-19 05:29:19] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.590007, 0.1), 'l2norm': (0.793805, 0.1)}.
[INFO 08-19 05:29:22] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.43976, 'x2': 0.249591, 'x3': 0.358308, 'x4': 0.276712, 'x5': 0.181191, 'x6': 0.570191}.
[INFO 08-19 05:29:22] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.053806, 0.1), 'l2norm': (0.892606, 0.1)}.
[INFO 08-19 05:29:24] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.48169, 'x2': 0.233968, 'x3': 0.346338, 'x4': 0.251994, 'x5': 0.207013, 'x6': 0.616993}.
[INFO 08-19 05:29:24] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.920198, 0.1), 'l2norm': (0.933605, 0.1)}.
[INFO 08-19 05:29:25] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.445272, 'x2': 0.293305, 'x3': 0.380047, 'x4': 0.257935, 'x5': 0.236099, 'x6': 0.602089}.
[INFO 08-19 05:29:26] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.382106, 0.1), 'l2norm': (0.887109, 0.1)}.
[INFO 08-19 05:29:28] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.418006, 'x2': 0.337878, 'x3': 0.396759, 'x4': 0.246439, 'x5': 0.271045, 'x6': 0.613299}.
[INFO 08-19 05:29:28] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.223975, 0.1), 'l2norm': (0.873275, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 08-19 05:29:28] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 41.67 seconds.